from CvPythonExtensions import *

import GerikSpells
import GerikSpellsUtils
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSMission as GSResults

from GSTargetInfoPolicies import PLOT_RANGE_AFFECT_RULE_SELECTED as PLOT_RANGE_AFFECT_RULE
from GSTargetInfoPolicies import GSSquarePlotTarget

from GSGraphicsPolicies import GSPlot

gc = CyGlobalContext()

PLOT_SQUARE_SIZE = 1

class Earthquake(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self, GSSquarePlotTarget(self.canTargetPlot, PLOT_SQUARE_SIZE))

    def canUnitCast(self, pCastingUnit, pCastingPlot, bVisible):
        if (pCastingUnit.getUnitType() == gc.getInfoTypeForString("UNIT_WARRIOR")):
            return True

    def createTargettingStageList(self, bAlt, bShift, bCtrl):
        pPlotTargetPolicy = GSSquarePlotTarget(self.canTargetPlot, PLOT_SQUARE_SIZE)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotTargetPolicy.setCheckRange(True)
        pPlotTargetPolicy.setRangeAffectRule(PLOT_RANGE_AFFECT_RULE)
        
        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setAffectedPlotColor("COLOR_PLAYER_BROWN", 1.0)
        pPlotGraphicsPolicy.setAffectedPlotTexture(PlotStyles.PLOT_STYLE_WAVES)
        pPlotGraphicsPolicy.setUnaffectedPlotColor("COLOR_WHITE", 1.0)
        pPlotGraphicsPolicy.setUnaffectedPlotTexture(PlotStyles.PLOT_STYLE_WAVES)
        pPlotGraphicsPolicy.setShowRangeGraphics(True)

        return [ (pPlotTargetPolicy, pPlotGraphicsPolicy) ]

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pPotentialTarget, bVisible):
        return True


    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, 3)

    def startSpellResults(self, pCastingGroup, pTargetInfo): # pTargetInfo is a GSSquarePlotTarget object.
        CyEngine().triggerEffect(gc.getInfoTypeForString("EFFECT_RES_BOMB"), pTargetInfo.getTarget().getPoint())
        GSResults.startSpellResults(self, pCastingGroup, pTargetInfo)

    def startSpellResultsOnTarget(self, pCastingGroup, pTarget): # pTarget is a plot
        iUnits = pTarget.getNumUnits()
        for i in range(0, iUnits):
            pUnit = pTarget.getUnit(i)
            if (pUnit.getDomainType() != gc.getInfoTypeForString("DOMAIN_AIR")):
                iChangeHitPoints = (pUnit.maxHitPoints() / 5) + 1 # Roundoff
                pUnit.changeDamage(iChangeHitPoints, pUnit.getOwner())
